from django.contrib import admin
from django.contrib.admin.models import LogEntry
from MyBlog.my_admin_site import my_site
from app.models import (User, messageTable, BackgroundMusic, Message)


# 构建一个公共基类，设置默认配置项
class MyBaseAdmin():
    save_on_top = True
    save_on_bottom = True
    list_select_related = True
    save_as = True
    list_display = ['id']


@admin.register(User, site=my_site)
class UserAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'username', 'mobile', 'job', 'desc', 'isVistor', 'date_joined']

    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 信息表
@admin.register(messageTable, site=my_site)
class messageTableAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'senderId', 'receiverId', 'content', 'messageTime', 'isRead', 'isShow']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 操作日志
@admin.register(LogEntry, site=my_site)
class LogEntryAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'object_repr', 'object_id', 'action_flag', 'user', 'change_message']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 歌曲
@admin.register(BackgroundMusic, site=my_site)
class BackgroundMusicAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'title', 'picture', 'file_path']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段


# 留言
@admin.register(Message, site=my_site)
class MessageAdmin(admin.ModelAdmin, MyBaseAdmin):
    list_display = ['id', 'user', 'content', 'date', 'weight']
    list_display_links = list_display[:1]  # 默认的链接是id
    search_fields = list_display  # 搜索的字段同list_display
    list_filter = list_display[1:3]  # 过滤字段为前3个字段
